package org.seed.rpc.core;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class RuntimeLocal {


    /**
     * 服务名与服务与可用服务节点集合
     * <p>
     * key: service-name
     * value: endpoint (List)
     */
    private static Map<String, List<String>> serviceNameEndpointMap = new HashMap<>();


    /**
     * 根据服务名称获取可路由的服务节点
     *
     * @param serviceName 服务名称
     */
    public static List<String> getEndpointList(String serviceName) {
        Assert.notNull(serviceName, "service-name can't be null.");
        return serviceNameEndpointMap.get(serviceName);
    }

    /**
     * 将服务与服务节点存储至本地
     */
    public static void cacheLocalServiceNameEndpointMap(Map<String, List<String>> serviceNameEndpointMap) {
        RuntimeLocal.serviceNameEndpointMap = serviceNameEndpointMap;
        log.info("Local caching data: \n{}", new Gson().toJson(serviceNameEndpointMap));
    }


    private static RegistryClient registryClient;

    /**
     * 设置注册中心客户端
     */
    public static void setRegistryClient(RegistryClient registryClient) {
        RuntimeLocal.registryClient = registryClient;
    }

    /**
     * 获取注册中心客户端
     */
    public static RegistryClient getRegistryClient() {
        return registryClient;
    }


}
